package com.ruoyi.mobile.api;


import com.ruoyi.common.core.domain.AjaxResults;
import com.ruoyi.framework.app.service.IKUsersFraneworkService;
import com.ruoyi.qsng.domain.KUsers;
import com.ruoyi.qsng.dto.UserGrantDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * 登录控制器
 *
 * @author gk
 * @date 2023-06-12
 */
@Api(tags = "登录控制器")
@RestController
@RequestMapping
public class LoginController {

    @Autowired
    private IKUsersFraneworkService ikUsersFraneworkService;



    @ApiOperation("判断用户是否需要授权获取手机号, true代表需要，false代表不需要")
    @PostMapping("/app/isNeedGrantPhone")
    public AjaxResults<UserGrantDto> isNeedGrantPhone(@RequestParam String code) {
        return ikUsersFraneworkService.isNeedGrantPhone(code);
    }

    @ApiOperation("小程序微信授权登录入口")
    @PostMapping("/app/login")
    public AjaxResults<KUsers> appLogin(@RequestParam String openid, @RequestParam String rawData,
                                       @RequestParam(required = false) String encryptedData, @RequestParam(required = false) String iv) {
        return ikUsersFraneworkService.appLogin(openid, rawData, encryptedData, iv);
    }

    @ApiOperation("小程序手机号密码登录入口")
    @PostMapping("/app/phoneLogin")
    public AjaxResults<KUsers> appPhoneLogin(@RequestParam String username, @RequestParam String password) {
        return ikUsersFraneworkService.appPhoneLogin(username, password);
    }

    @ApiOperation("小程序端修改登录密码")
    @PostMapping("/app/changePassword")
    public AjaxResults<KUsers> appChangePassword(@RequestParam String oldPassword, @RequestParam String newPassword,
                                              @RequestParam String newPasswordConfirm) {
        return ikUsersFraneworkService.appChangePassword(oldPassword, newPassword, newPasswordConfirm);
    }


    @ApiOperation("小程序退出登录")
    @DeleteMapping("/app/loginOut")
    public AjaxResults<String> apploginOut(HttpServletRequest request) {
        return ikUsersFraneworkService.apploginOut(request);
    }

    @ApiOperation("不要调用此接口，后台开发人员测试登录小程序")
    @PostMapping("/app/loginDev")
    public AjaxResults<KUsers> login(@RequestParam String telephone) {
        String openId = "123456789";
        return ikUsersFraneworkService.devLogin(openId, telephone);
    }

}
